﻿using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Security.AccessControl;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using ZoneLine.Models;
using ZoneLine.PlcMaster;
using ZoneLine.Stations.HStation;

namespace ZoneLine.Stations
{
    public class HOP010Station : HStationBase
    {      
        public HOP010Station() {
            _logUiChannel = nameof(HOP010Station) + "uilog";
        }

        private HOP010StationResult _stationRecordData;

        public new HOP010StationResult StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override void DealWithSignal(string name)
        {

            if (name == nameof(StartUploadCode))
            {
                //todo 上传了条码 这时候验证是否要工作
                if (StartUploadCode)
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位结论验证,条码信息是{StationRecordData.AbsScanCode}");
                    var validataRlt = Validata(CurrentStationName, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位的验证结果是{validataRlt}");
                    var writeRlt = WriteWorkActionToPlc(AllowWorkAddr, validataRlt);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位向下位机下发消息，下发{writeRlt}");
                    return;
                }
                else
                {
                    WriteData(EndUpLoadCodeAddr, false);
                    WriteData(AllowWorkAddr, 0);
                    return;
                }
            }

            if (name == nameof(StartUploadData))
            {
                if (StartUploadData)
                {
                    //todo 上传数据 这时候将数据记录到数据库中
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到记录数据命令");
                    var rlt = RecordData();
                    WriteData(EndUpLoadDataAddr, rlt == 1);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}记录数据完成");
                }
                else
                {
                    WriteData(EndUpLoadDataAddr, false);
                }
            }
        }

        protected override int RecordData()
        {
            var result = new HOP010StationResult()
            {
                AbsScanCode = StationRecordData.AbsScanCode,
                BushScanCode = StationRecordData.BushScanCode,
                StationCode = this.StationCode,
                PartType = StationRecordData.PartType,
                Result = StationRecordData.Result,
                PressPosition = StationRecordData.PressPosition,
                PressForce = StationRecordData.PressForce,
                CycleTime = StationRecordData.CycleTime,
            };
            var writeRlt = false;
            if (_dal.CheckExist<HOP010StationResult>(p => p.AbsScanCode == result.AbsScanCode))
            {
                var beforeRlt = _dal.GetModel<HOP010StationResult>(p => p.AbsScanCode == result.AbsScanCode);
                result.Id = beforeRlt.Id;
                writeRlt = _dal.UpdateModelSuper<HOP010StationResult>(p => p.AbsScanCode == result.AbsScanCode, result);
            }
            else
            {
                writeRlt = _dal.AddModel(result);

            }
            return writeRlt ? 1 : 2;
        }

        protected override void DealData(byte[] content)
        {
            StationRecordData.AbsScanCode = GetPlcString(content, 26);
            StationRecordData.BushScanCode = GetPlcString(content, 58);
            StationRecordData.PartType = GetPlcString(content, 90);
            StationRecordData.Result = GetPlcString(content, 102);
            StationRecordData.PressPosition = _client.ByteTransform.TransSingle(content, 114);
            StationRecordData.PressForce = _client.ByteTransform.TransSingle(content, 118);
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 122);
            StationRecordData.BushScanCode2 = GetPlcString(content, 150);
        }
    }
}
